package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
@Slf4j
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    PermissionDao permissionDao;



    /**
     * 查询所有权限
     * @return
     */
    @Override
    public List<Permission> findAll() {

        return permissionDao.selectAll();
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        log.info("[检查组-分页查询]data：{}",queryPageBean);
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //查询page
        Page<Permission> page = permissionDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Permission permission) {
        permissionDao.insert(permission);
    }

    @Override
    public void edit(Permission permission) {
        permissionDao.update(permission);
    }

    @Override
    public Permission findById(Integer id) {

        return permissionDao.selectById(id);
    }

    @Override
    public void delete(Integer id) {
        long number = permissionDao.countCheckPermissionByid(id);
        if(number>0){
            throw new BusinessRuntimeException("当前权限已有关联数据 不能删除");
        }
        permissionDao.delete(id);
    }

    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer id) {

        return  permissionDao.selectPermissionIdsByRoleId(id);
    }


}
